package ar.edu.isft189.pdfBox;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Parsing XML File utilizando un unico Map para representar los valores
* de los distintos tags del mismo
*
* @author Guillermo Cherencio
*/
public class HashHandleXMLFile extends DefaultHandler {
    private Map<String,String> map = new HashMap<String,String>();
    private Set<String> headerForm = new LinkedHashSet<String>();//Campos Cabecera
    /*
     * Se usa el linkedHashSet por que no permite repetidos y matiene el orden en
     * que aparecen los datos a diferencia de el Set que no respeta el orden
     */
    private Set<String> headerGrid  = new LinkedHashSet <String>();// Nombre de las columnas
     private Set<String> fieldAllSet  = new LinkedHashSet <String>();// Todos los nombres de los campos
  
    private boolean bId = false;
    private boolean bRname = false;
    private boolean bIsft = false;
    private boolean bName = false;
    private boolean bInternal = false;
    private boolean bValue = false;
    private boolean brNumber = false;
    private boolean bRinternal = false;
    private boolean bFields = false;
    private boolean bNrows = false;
    private boolean bNrow = false;
    private boolean bLang = false;
    private String sinternal="";
    private String sName="";
    private String rNumber="";
    private Integer a=1;
    
    public HashHandleXMLFile() {
        super();
    }

    public HashHandleXMLFile(Map<String,String> m) { 
        super();
        map = m;
     }
    // comienza tag

    public void startElement(String uri, String localName,String qName,Attributes attributes) throws SAXException {
        //System.out.println("uri="+uri+" localName="+localName+" qName="+qName);
        if(qName.equalsIgnoreCase("id")){
            bId= true;
        }else{
            if(qName.equalsIgnoreCase("nrows")){
                bNrows= true;
            }else{
                if(qName.equalsIgnoreCase("rname")){
                    bRname= true;
                }else{
                    if(qName.equalsIgnoreCase("name")){
                        bName=true;
                    }else{                        
                        if(qName.equalsIgnoreCase("language")){
                            bLang=true;   
                        }else{
                            if(qName.equalsIgnoreCase("internal")){
                                bInternal=true;   
                            }else{
                                if(qName.equalsIgnoreCase("value")){
                                    bValue=true;   
                                }else{
                                    if(qName.equalsIgnoreCase("rNumber")){
                                        brNumber=true;   
                                    }else{
                                        if(qName.equalsIgnoreCase("rinternal")){
                                            bRinternal=true;   
                                        }else{
                                            if(qName.equalsIgnoreCase("field")){
                                                bFields=true;   
                                            }
                                        }
                                    }
                                }
                            }                        
                        }
                    }
                }
            }
        }
    }

    @Override
    public void endElement(String uri, String localName,String qName) throws SAXException {
        if(qName.equalsIgnoreCase("id")){
            bId=false;
        }else{
            if(qName.equalsIgnoreCase("rname")){
                bRname= false;
            }else{
                if(qName.equalsIgnoreCase("nrows")){
                    bNrows= false;
                }else{
                     if(qName.equalsIgnoreCase("language")){
                            bLang = false;   
                     }else{
                         if( qName.equalsIgnoreCase("name")){
                            bName = false;
                        }else{                 
                            if(qName.equalsIgnoreCase("internal")){
                                bInternal=false;   
                            }else{
                                 if(qName.equalsIgnoreCase("value")){
                                    bValue=false;   
                                }else{
                                    if(qName.equalsIgnoreCase("rNumber")){
                                        brNumber=false;   
                                    }else{
                                        if(qName.equalsIgnoreCase("rinternal")){
                                            bRinternal=false;   
                                        }else{
                                            if(qName.equalsIgnoreCase("fields")){
                                                bFields=false;   
                                            }
                                        }
                                    }
                                }
                            }                        
                        }
                    }
                }
            }
        }
    }

    @Override
    public void characters(char ch[], int start, int length) throws SAXException {
        String contenido_tag = new String(ch, start, length);
        if(bId){
           map.put("id",contenido_tag);    
           return;
        }else{
            if(bRname){
                map.put("rname",contenido_tag);    
                return;
            }else{
                if(bNrows){
                    map.put("nrows",contenido_tag);
                    return;
                }else{
                    if(bLang){
                        map.put("languaje",contenido_tag);
                        return;
                    }else{
                        if(bName){
                            sName=contenido_tag;
                            fieldAllSet.add(sName);
                            return;
                        }else{                       
                            if(bInternal){
                                map.put("field."+sName+".internal",contenido_tag);
                                headerForm.add(sName);
                                return;
                            }else{
                                if(bValue){
                                    map.put("field."+sName+".internal.value",contenido_tag);
                                    return;
                                }else{
                                    if(brNumber){
                                        rNumber=contenido_tag;                                         
                                        return;
                                    } else{
                                        if(bRinternal){
                                            String header = "fields."+sName+".rows."+rNumber+".rinternal";
                                            headerGrid.add(sName);
                                            map.put(header,contenido_tag);                                        
                                            return;
                                        }
                                    } 
                                }
                            }                    
                        }
                    }
                }
            }
        }
    }

    public Map<String,String> getMap() { 
        return this.map;
    }
    
    public Set<String> getHeaderForm() { 
        return this.headerForm;
    }
    
    public Set<String> getHeaderGrid(){
        return this.headerGrid;
    }
    
    public Set<String> getFieldAllSet(){
        return this.fieldAllSet;
    }
}
